package com.aso114.project.mvp.activity.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.aso114.project.R;
import com.aso114.project.adapter.CollectAdapter;
import com.aso114.project.base.BaseFragment;
import com.aso114.project.bean.QuestionBean;
import com.aso114.project.bean.QuestionEvent;
import com.aso114.project.exam.QuestionTestActivity;
import com.aso114.project.mvp.activity.MyCollectInfoActivity;
import com.aso114.project.mvp.model.CallBack;
import com.aso114.project.mvp.model.CommentModel;
import com.aso114.project.util.Constant;
import com.aso114.project.util.Helper;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by asus on 2018/12/21.
 * 收藏-行测
 */

public class CollectLeftFragment extends BaseFragment implements CollectAdapter.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.my_collection_ly)
    LinearLayout myCollectionLy;
    @BindView(R.id.my_collection_listView)
    RecyclerView myCollectionListView;
    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout swipeRefreshLayout;
    Unbinder unbinder;

    QuestionBean qdata;
    CollectAdapter collectAdapter;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.fragment_collext;
    }

    @Override
    protected void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        //设置布局管理器
        myCollectionListView.setLayoutManager(layoutManager);
        collectAdapter = new CollectAdapter(getActivity());
        myCollectionListView.setAdapter(collectAdapter);
        getData();
        collectAdapter.setOnItemClickListener(this);
        swipeRefreshLayout.setOnRefreshListener(this);
    }

    private void getData() {
        swipeRefreshLayout.setRefreshing(true);
        CommentModel.getInstant().getCollect(Constant.adutst, new CallBack() {
            @Override
            public void onSuccess(Object o, boolean isSuccess, String msg) {
                swipeRefreshLayout.setRefreshing(false);
                Helper.gotoLogin(isSuccess);//token失效去重新登陆
                qdata = (QuestionBean) o;
                if (qdata == null || qdata.getQuestionBaseList().get(0).getQuestionList().size() == 0) {
                    myCollectionLy.setVisibility(View.VISIBLE);
                    swipeRefreshLayout.setVisibility(View.GONE);
                    return;
                } else {
                    myCollectionLy.setVisibility(View.GONE);
                    swipeRefreshLayout.setVisibility(View.VISIBLE);
                    collectAdapter.setData(qdata.getQuestionBaseList().get(0).getQuestionList());
                    collectAdapter.notifyDataSetChanged();
                }

            }

            @Override
            public void onFailure(String fail) {
                Helper.showToast(fail);
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void addOnClick() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onItemClick(int position) {
        Log.i("yxp", position + "----");
        EventBus.getDefault().postSticky(qdata);
        Constant.title_tv = "8";//从收藏进入的
        Intent intent = new Intent(getActivity(), QuestionTestActivity.class);
        intent.putExtra("isAnalysis", true);
        intent.putExtra("position", position);
        intent.putExtra(QuestionTestActivity.IS_EXAM, false);
        startActivity(intent);
    }

    @Override
    public void onItemClearClick(final int position) {
        CommentModel.getInstant().delCollect(Constant.adutst,qdata.getQuestionBaseList().get(0).getQuestionList().get(position).getRealQuestionId(), new CallBack() {
            @Override
            public void onSuccess(Object o, boolean isSuccess, String msg) {
                Helper.gotoLogin(isSuccess);//token失效去重新登陆
//                Helper.showToast("取消收藏");
                getData();
            }

            @Override
            public void onFailure(String fail) {

            }
        });
    }

    @Override
    public void onRefresh() {
        getData();
    }
}
